const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      pickerOptions: pickerOptions,
      rules: {
        TotalAmount: [{ required: true, message: '请输入待分账金额', trigger: 'blur' }],
        Account: [{ required: true, message: '请输入付款账号', trigger: 'blur' }],
      },
      Supplier_SerialNumber: [{ required: true, message: '请选择供应商', trigger: 'change' }],
      AllocationRatio: [{ required: true, message: '请输入百分比', trigger: 'blur' }],
      Account: [{ required: true, message: '请输入付款账号', trigger: 'blur' }],
      Account: [{ required: true, message: '请输入付款账号', trigger: 'blur' }],
      store: [],
      column: [],
      shop: [],
      store: [],
      supplier: [],
      isDisabled: false,
      type: '',
      //导入属性
      fileList: [],
      sales_unitprice: [],
      Store_SerialNumber: [],
      Start_Date: '',
      End_Date: '',
      //
      Distribute: '',
      dialogVisible: false,
      always: '00:00:00',
      form: {     //分账管理
        Sales_Unit_Price_SerialNumber: "",  //主键
        TotalAmount: '',  //总金额
        Account: '',   //账户
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        Children: {
          sales_unitprice_detail: [{
            Sales_Unit_Price_Detail_SerialNumber: "",   //主键
            Supplier_SerialNumber: '',  //供应商
            EndDate: '',   //结算日期
            AllocationRatio: '',  //分配比例
            CollectionAccount: '',  //收款账户
          }],
          sales_unitprice_storescope: [{     //门店信息
            Sales_Unit_Price_StoreScope_SerialNumber: "",  //主键
            Store_SerialNumber: '',    //门店
            EndDate: '',   //结算日期 
            AllocationRatio: '',  //分配比例
            CollectionAccount: '',  //收款账户
          }],
        }
      },
      loading: false
    }
  },
  mounted() {
    this.getData()
  },
  computed: {
    Column() {
      return function (name) {
        let obj = this.column.filter((item, index) => {
          return name == item.name
        })[0]
        return obj.ShopColumn_Id
      }
    },
    Article() {
      return function (name) {
        let obj = this.shop.filter((item, index) => {
          return name == item.Article_Name
        })[0]
        return obj.Article_SerialNumber
      }
    },
    Unit_Price() {
      return function (name) {
        let obj = this.shop.filter((item, index) => {
          return name == item.Article_Name
        })[0]
        return obj.Unit_Price
      }
    },
  },
  methods: {
    getData() {
      let _this = this;
      //获取门店信息
      $.ajax({
        type: 'POST',
        url: DomainName + "/getInquiryAction",
        data: {
          Data: "store_info",
          token: $.cookie("token")
        },
        success: function (res) {
          res = res.filter(function (item) {
            return item.Row_Status != '1'
          })
          for (let i = 0; i < res.length; i++) {
            _this.store.push(res[i]);
          }
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
      //获取供应商
      $.ajax({
        type: 'POST',
        url: DomainName + "/getInquiryAction",
        data: {
          Data: "supplier_info",
          token: $.cookie("token")
        },
        success: function (Result) {
          console.log(Result);
          _this.supplier = Result      //将获取的数据存到表格数据中
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    // 校验日期
    validateEnd_Date(rule, value, callback) {
      if (+new Date(value) < +new Date(this.form.Start_Date)) {
        callback(new Error('结算时间不能小于开始时间'));
      } else {
        callback();
      }
    },
    Change_Unit_Price(index) {
      this.isDisabled = true
      let str = this.form.Children.sales_unitprice_detail[index].Article_SerialNumber
      for (let i = 0; i < this.shop.length; i++) {
        if (this.shop[i].Article_SerialNumber == str) {
          this.form.Children.sales_unitprice_detail[index].Unit_Price = this.shop[i].Unit_Price
        }
      }
    },
    //提交表单
    submitForm(formName) {
      let _this = this
      console.log(this.form);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // _this.loading = true
        } else {
          console.log("error submit!!");
          return false;
        }
      })
    },
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    onExcel() {
      this.dialogVisible = true
    },
    // 增加供应商信息
    add_sales_unitprice_detail() {
      this.form.Children.sales_unitprice_detail.push({     //供应商
        Sales_Unit_Price_Detail_SerialNumber: "",   //主键
        Supplier_SerialNumber: '',  //供应商
        EndDate: '',   //结算日期
        AllocationRatio: '',  //分配比例
        CollectionAccount: '',  //收款账户
      })
    },
    // 删除供应商
    remove_sales_unitprice_detail() {
      if (this.form.Children.sales_unitprice_detail.length > 1) {
        this.form.Children.sales_unitprice_detail.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    // 增加门店信息
    add_sales_unitprice_storescope() {
      this.form.Children.sales_unitprice_storescope.push({
        Sales_Unit_Price_StoreScope_SerialNumber: "",  //主键
        EndDate: '',   //结算日期
        AllocationRatio: '',  //分配比例
        CollectionAccount: '',  //收款账户
      })
    },
    // 删除门店
    remove_sales_unitprice_storescope() {
      if (this.form.Children.sales_unitprice_storescope.length > 1) {
        this.form.Children.sales_unitprice_storescope.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    beforeRemove(file, fileList) {
      this.fileList.pop()
    },
    // 导入
    uploadDetails(file) {
      this.fileList.push({
        name: file.name,
        url: file.type
      })
      let _this = this
      var reader = new FileReader();
      reader.readAsBinaryString(file);
      reader.onload = function (e) {
        var data = e.target.result;
        var wb = XLSX.read(data, {
          type: 'binary' //以二进制的方式读取
        });
        var sheet0 = wb.Sheets[wb.SheetNames[0]];//sheet0代表excel表格中的第一页
        var str = XLSX.utils.sheet_to_json(sheet0);//利用接口实现转换。
        console.log(str);
        //将整体数据分解成每一条，一条上传一次
        let newArr = []
        //1.将接受到数组便利
        //2.将栏目存到_this.sales_unitprice里并去重
        str.forEach((item, index) => {
          if (newArr.indexOf(item['所属栏目']) == -1) {
            newArr.push(item['所属栏目'])//根据这个数组判断是否重复
            _this.sales_unitprice.push({
              Sales_Unit_Price_SerialNumber: "",
              Start_Date: "",
              End_Date: "",
              ShopColumn_Id: _this.Column(item['所属栏目']),
              Children: {
                sales_unitprice_detail: [],
                sales_unitprice_storescope: []
              }
            })
          }
          //3.将商品等信息添加到对应栏目下的sales_unitprice_detail
          _this.sales_unitprice.forEach((item1, index1) => {
            if (_this.Column(item['所属栏目']) == item1.ShopColumn_Id) {
              item1.Children.sales_unitprice_detail.push({
                Sales_Unit_Price_Detail_SerialNumber: "",
                Sales_Unit_Price_SerialNumber: '',
                Article_SerialNumber: _this.Article(item['商品编号']),
                Unit_Price: _this.Unit_Price(item['商品编号']),
                Sales_Unit_Price: item['销售单价'],
                Sign: item['有/无货货主'],
                Distribute: parseInt(item['分配数量']),
                Children: {
                  sales_unitprice_stock: []
                }
              })
            }
          })
        })
      }
    },
  }
})